package com.kaikeba.wx.controller;

import com.kaikeba.bean.CourierExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.UserExpress;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierExpressService;
import com.kaikeba.service.ExpressService;
import com.kaikeba.service.UserExpressService;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.SMSUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class AddExpressController {
    @ResponseBody("/wx/addExpress.do")
    public String addExpress(HttpServletRequest request, HttpServletResponse response){
        //获取参数值
        String number = request.getParameter("number");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String company = request.getParameter("company");
        //获取登陆人号码
        UserExpress wxUser = UserUtil.getWxUser(request.getSession());
        //判断快递单号是否存在  调取service方法
        Express byNumber = ExpressService.findByNumber(number);
        Message msg = new Message();
        if(byNumber == null && wxUser.getUserPhone() != null){
            //证明快递单号不存在，可以存
            Express e = new Express(number,username,userPhone,company,wxUser.getUserPhone());
            boolean insert = ExpressService.insert(e);

            //存入之后获取取件码
            if(insert){
                Express e2 = ExpressService.findByNumber(number);
                String code = e2.getCode();
                //发送短信给用户
                boolean flag = SMSUtil.addExpressSend(userPhone,code);
                if(flag){
                    msg.setStatus(0);
                    msg.setResult("快递录入成功,短信已发送给对方");
                }else {
                    msg.setStatus(-1);
                    msg.setResult("短信发送失败，请检查后重新录入");
                    //录入失败就把它删除
                    Express e3 = ExpressService.findByNumber(number);
                    ExpressService.delete(e3.getId());
                }
            }else{
                msg.setStatus(-2);
                msg.setResult("录入失败,请检查后在录入");
                //录入失败就把它删除
            }
        }else {
            msg.setStatus(-3);
            msg.setResult("录入失败,单号可能重复,请检查");
        }
        return JSONUtil.toJSON(msg);

    }

    @ResponseBody("/wx/update.do")
    public String updateExpress(HttpServletRequest request, HttpServletResponse response) {
        //获取现在用户登录的手机号   因为是有了之前的信息才能跳转到此页面
        UserExpress wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getUserPhone();
        //获取状态码判断是否是快递员
        int flagStatus = UserUtil.getStatus(request.getSession());
        //调用service方法   用户的
        UserExpress userExpress = UserExpressService.findByPhone(userPhone);
        //快递员的
        CourierExpress courierExpress = CourierExpressService.findByPhone(userPhone);
        //获取前台传来的信息
        String username = request.getParameter("username");
        String phone = request.getParameter("userPhone");


        UserExpress userPhoneExpress = UserExpressService.findByPhone(phone);
        CourierExpress courierPhoneExpress = CourierExpressService.findByPhone(phone);
        //获取验证码
        String code = request.getParameter("code");
        //先判断验证码是否一样
        //获取后端发送的验证码  然后判断两个验证码是否正确
        String sysCode = UserUtil.getLoginSms(request.getSession(), phone);
        Message msg = new Message();
        if (sysCode.equals(code)) {
            //证明输入的验证码正确
            //然后判断该用户是快递员还是用户   都是的话就都改
            if (flagStatus == 1) {
                //证明现在登陆的是快递员
                if (userExpress != null) {
                    //证明都有  就都改
                    if (phone == userPhone || userPhoneExpress == null) {
                        UserExpress e = new UserExpress(username, phone, userExpress.getIDCard(), userExpress.getPassword());
                        boolean update = UserExpressService.update(userExpress.getId(), e);
                        msg.setResult(update + "");
                        msg.setStatus(0);
                    } else {
                        //否则就是号码已经有人使用
                        msg.setResult("此号码已被人使用");
                        msg.setStatus(-1);
                    }

                }
                if (phone == userPhone || courierPhoneExpress == null) {
                    CourierExpress e = new CourierExpress(username, phone, courierExpress.getIDCard(), courierExpress.getPassword());
                    boolean update = CourierExpressService.update(courierExpress.getId(), e);
                    msg.setResult(update + "");
                    msg.setStatus(0);
                } else {
                    //否则就是号码已经有人使用
                    msg.setResult("此号码已被人使用");
                    msg.setStatus(-1);
                }
            } else {
                //证明登陆的是用户   只改用户
                if (phone == userPhone || userPhoneExpress == null) {
                    UserExpress e = new UserExpress(username, phone, userExpress.getIDCard(), userExpress.getPassword());
                    boolean update = UserExpressService.update(userExpress.getId(), e);
                    msg.setResult(update + "");
                    msg.setStatus(0);
                } else {
                    //否则就是号码已经有人使用
                    msg.setResult("此号码已被人使用");
                    msg.setStatus(-1);
                }
            }
        }else{
            msg.setStatus(-1);
            msg.setResult("验证码不正确");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/findByUNumber.do")
    public String findByNumber(HttpServletRequest request, HttpServletResponse response){
        //获取登录的信息
        UserExpress wxUser = UserUtil.getWxUser(request.getSession());
        //获取快递单号
        String number = request.getParameter("expressNum");
        //调取service查询
        Express e = ExpressService.findByNumber(number);
        Message msg = new Message();
        if(e == null || !e.getUserPhone().equals(wxUser.getUserPhone())){
            //没快递信息
            msg.setStatus(-1);
            msg.setResult("无快递信息");
        }else{
            //有快递信息
            msg.setStatus(0);
            msg.setResult("查询成功");
            //存值
            UserUtil.setExpress(request.getSession(),e);
        }
        return JSONUtil.toJSON(msg);

    }


    @ResponseBody("/wx/findExpressByNumberList.do")
    public String findByNumberList(HttpServletRequest request, HttpServletResponse response){
        //获取值
        Express express = UserUtil.getExpress(request.getSession());
        Message msg = new Message();
        if(express == null){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(express);
        return JSONUtil.toJSON(msg);
    }
}
